***************************************
*Analysis of IV monotonicity assumption
***************************************

clear all

*main directory
global mydir "C:\Users\ajsaenz\Dropbox (JPAL LAC)\Preu\"

cd "${mydir}\Preu"
use "${mydir}\Preu\data\base_preu_final.dta"
**************************************
* Some new variables and housekeeping*
**************************************

egen real_group=group(t_grupo_lev5)
g t_cohorte_grupo=t_cohorte*1000000+real_group
drop real_group
replace t_completed=0 if t_completed==.

*Interactions
gen bl_school_quality_high=0 if bl_school_quality_2~=.
replace bl_school_quality_high=1 if bl_school_quality_2==2

forvalues y=0/1 {
forvalues x=0/1 {
gen int_ols_c`y'_s`x'=0 if bl_school_quality_high~=.
replace int_ols_c`y'_s`x'=t_itt if t_cohorte==`y' & bl_school_quality_high==`x'
gen int_iv_c`y'_s`x'=0  if bl_school_quality_high~=.
replace int_iv_c`y'_s`x'=t_completed if t_cohorte==`y' & bl_school_quality_high==`x'
gen int_top_c`y'_s`x'=0  if bl_school_quality_high~=. & el_prep_sum_toppreu~=.
replace int_top_c`y'_s`x'=el_prep_sum_toppreu if t_cohorte==`y' & bl_school_quality_high==`x'
}
gen int_ols_c`y'=0
replace int_ols_c`y'=t_itt if t_cohorte==`y'
gen int_iv_c`y'=0
replace int_iv_c`y'=t_completed if t_cohorte==`y'
gen int_top_c`y'=0 if el_prep_sum_toppreu~=.
replace int_top_c`y'=el_prep_sum_toppreu if t_cohorte==`y'
gen int_ols_s`y'=0 if bl_school_quality_high~=.
replace int_ols_s`y'=t_itt if bl_school_quality_high==`y'
gen int_iv_s`y'=0 if bl_school_quality_high~=.
replace int_iv_s`y'=t_completed if bl_school_quality_high==`y'
gen int_top_s`y'=0 if bl_school_quality_high~=. & el_prep_sum_toppreu~=.
replace int_top_s`y'=el_prep_sum_toppreu if bl_school_quality_high==`y'
}
gen int_c1_s1=0 if bl_school_quality_high~=.
replace int_c1_s1=1 if t_cohorte==1 & bl_school_quality_high==1

***PERCENT CORRECT ANSWERS IN MATH
g psucorrect_percent=(ela_psucorrect_math/70) if t_cohorte==0
replace psucorrect_percent=(ela_psucorrect_math/75) if t_cohorte==1 
replace psucorrect_percent=psucorrect_percent*70

//Part Z.1. Differences between groups
gen bla_psuscore_science=bla_psuscore_math
foreach x in leng math science {
gen t_peer_`x'_group_high=0 if t_peer_`x'_group~=.
replace t_peer_`x'_group_high=1 if t_peer_`x'_group==1
gen t_peer_`x'_group_mixed=0 if t_peer_`x'_group~=.
replace t_peer_`x'_group_mixed=1 if t_peer_`x'_group==3
gen b_`x'=bla_psuscore_`x'
gen b_`x'_2=b_`x'*b_`x'
gen b_`x'_3=b_`x'*b_`x'*b_`x'
}


******************************************************************************
******************************************************************************

global X  "bla_psuscore_math  i.bl_ingreso bla_nota_dev bl_school_quality_high"

reg el_prep_sum_toppreu $X if t_itt==0
predict PT 

*Full sample
foreach gr in 10 {
sort PT
gen g`gr'=group(`gr')
clear matrix
matrix A=J(`gr',4,0)

forvalues i=1/`gr' {
qui: reg el_prep_sum_toppreu t_itt if g`gr'==`i'
local dif_pt_`i'=_b[t_itt]
local se_pt_`i'=_se[t_itt]
local ciup_`i'=`dif_pt_`i''+1.96*`se_pt_`i''
local cidn_`i'=`dif_pt_`i''-1.96*`se_pt_`i''

matrix A[`i',2]=`dif_pt_`i''
matrix A[`i',3]=`ciup_`i''
matrix A[`i',4]=`cidn_`i''
matrix A[`i',1]=`i'
}

preserve
svmat A
keep A*
drop if A1>`gr'
twoway (line A2 A1) (line A3 A1, lcolor(red) lpattern(dash)) (line A4 A1, lcolor(red) lpattern(dash)), leg(off) ytitle("P(TP | Z=1) - P(TP | Z=0)") xtitle("Group[predicted P(TP)]") title("Full sample") sub("`gr' groups")
graph save Graph "C:\Users\aleja_000\Desktop\J-PAL\Preu\asaenz\monotonicity\full_sample_`gr'.gph", replace
restore

drop g`gr'
}

*2010 cohort
foreach gr in 10 {
sort PT
gen g`gr'=group(`gr')
clear matrix
matrix A=J(`gr',4,0)

forvalues i=1/`gr' {
qui: reg el_prep_sum_toppreu t_itt if g`gr'==`i' & t_cohorte==0
local dif_pt_`i'=_b[t_itt]
local se_pt_`i'=_se[t_itt]
local ciup_`i'=`dif_pt_`i''+1.96*`se_pt_`i''
local cidn_`i'=`dif_pt_`i''-1.96*`se_pt_`i''

matrix A[`i',2]=`dif_pt_`i''
matrix A[`i',3]=`ciup_`i''
matrix A[`i',4]=`cidn_`i''
matrix A[`i',1]=`i'
}

preserve
svmat A
keep A*
drop if A1>`gr'
twoway (line A2 A1) (line A3 A1, lcolor(red) lpattern(dash)) (line A4 A1, lcolor(red) lpattern(dash)), leg(off) ytitle("P(TP | Z=1) - P(TP | Z=0)") xtitle("Group[predicted P(TP)]") title("2010 cohort") sub("`gr' groups")
graph save Graph "C:\Users\aleja_000\Desktop\J-PAL\Preu\asaenz\monotonicity\2010cohort_`gr'.gph", replace
restore

drop g`gr'
}

*2011 coort
foreach gr in 10 {
sort PT
gen g`gr'=group(`gr')
clear matrix
matrix A=J(`gr',4,0)

forvalues i=1/`gr' {
qui: reg el_prep_sum_toppreu t_itt if g`gr'==`i' & t_cohorte==1
local dif_pt_`i'=_b[t_itt]
local se_pt_`i'=_se[t_itt]
local ciup_`i'=`dif_pt_`i''+1.96*`se_pt_`i''
local cidn_`i'=`dif_pt_`i''-1.96*`se_pt_`i''

matrix A[`i',2]=`dif_pt_`i''
matrix A[`i',3]=`ciup_`i''
matrix A[`i',4]=`cidn_`i''
matrix A[`i',1]=`i'
}

preserve
svmat A
keep A*
drop if A1>`gr'
twoway (line A2 A1) (line A3 A1, lcolor(red) lpattern(dash)) (line A4 A1, lcolor(red) lpattern(dash)), leg(off) ytitle("P(TP | Z=1) - P(TP | Z=0)") xtitle("Group[predicted P(TP)]") title("2011 cohort") sub("`gr' groups")
graph save Graph "C:\Users\aleja_000\Desktop\J-PAL\Preu\asaenz\monotonicity\2011cohort_`gr'.gph", replace
restore
drop g`gr'
}


*******************************************************
*Decomposition of IV Weighting by population subgroups*
*******************************************************

/* This program replicates Table 3 of Kling (2001). The weights of every subgroup Q of the population are given by w(Q), which is formed by
3 terms:
		1- deltaS=E[E(S|Z=1,X,Q)-E(S|Z=0,X,Q)|Q] : the efecto of the instrument Z over the variable of interest S (Top Preu attendance, in our case)
												   X denotes the base characteristics of the individuals.
		2- epvar=E[P(Z|X,Q)(1-P(Z|X,Q))|Q] : conditional variance of Z, where P(Z|X,Q) is the probability of Z=1 (being assigned to the treatment.
		3- p(Q) : share of the sample in subgroup Q	
	To construct w first we estimate terms 1, 2 and 3, and then we combine them according to Kling (2001, p. 4).
*/

global X  "bla_psuscore_math  i.bl_ingreso bla_nota_dev bl_school_quality_high"

*gen groups
sort PT
gen g10=group(10)
*gen group & group x instrument dummies
forvalues i=1/10 {
gen gr`i'=0
replace gr`i'=1 if g10==`i'
gen grxz`i'=0 if t_itt!=.
replace grxz`i'=t_itt if g10==`i'
}
order gr1 gr2 gr3 gr4 gr5 gr6 gr7 gr8 gr9 gr10 grxz1 grxz2 grxz3 grxz4 grxz5 grxz6 grxz7 grxz8 grxz9 grxz10, seq last 

*	1-	deltaS=E[E(S|Z=1,X,Q)-E(S|Z=0,X,Q)|Q]
reg el_prep_sum_toppreu gr1-gr10 grxz1-grxz10 $X

forvalues i=1/10 {
local s`i' = _b[grxz`i']
}

*	2-	epvar=E[P(Z|X,Q)(1-P(Z|X,Q))|Q]
reg t_itt gr1-gr10 $X
predict t_itt_hat
gen pvar = (t_itt_hat)*(1-t_itt_hat)
egen epvar = mean(pvar), by(g10)

forvalues i=1/10 {
sum epvar if g10==`i'
local ev`i' = r(mean)
}

*	3-	p(Q)
forvalues i=1/10 {
sum gr`i'
local p`i' = r(mean)
}

*	4-	w(Q)
local swq =(`ev1'*`p1'*`s1')+(`ev2'*`p2'*`s2')+(`ev3'*`p3'*`s3')+(`ev4'*`p4'*`s4')+(`ev5'*`p5'*`s5')+(`ev6'*`p6'*`s6')+ ///
		   (`ev7'*`p7'*`s7')+(`ev8'*`p8'*`s8')+(`ev9'*`p9'*`s9')+(`ev10'*`p10'*`s10')

forvalues i=1/10 {
local w`i' = `ev`i''*`p`i''*`s`i''/`swq'
}

*	Tabla
forvalues i=1/10 {
local s`i': di %6.2f `s`i''
local ev`i': di %6.2f `ev`i''
local p`i': di %6.2f `p`i''
local w`i': di %6.2f `w`i''
}

texdoc init ivweights.tex, replace
tex \begin{table}[t]
tex \centering
tex \caption{Decomposition of IV Weighting by Population Subgroup} 
tex \begin{tabular}{lcccc} \hline \hline \addlinespace
tex & $w_{q}$ & $\lambda_{q|x}$ & $\Delta S_{q|x}$ & $\omega_{q|x}$ \\
tex & (1) & (2) & (3) & (4) \\
tex \addlinespace \hline
forvalues i=1/10 {
tex q = `i' & `p`i'' & `ev`i'' & `s`i'' & `w`i'' \\
}
tex \addlinespace \hline
tex \end{tabular}
tex \par{
tex \begin{tabular}{p{16.5cm}}
tex \centering
tex \scriptsize{\textit{Notes:} $\omega_{q|x}=(w_q \lambda_{q|x} \Delta S_{q|x})/(\sum_q w_q \lambda_{q|x} \Delta S_{q|x})$ }\\
tex \end{tabular}}
tex \end{table}
























